package com.customization.shunsheng;

import cn.hutool.core.date.DateUtil;
import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.kq.cmd.leavetypes.EditLeaveTypesCmd;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.*;

/**
 * 修改假期类型信息，更新员工字段的值到假期类型建模表
 */
@CommandDynamicProxy(target = EditLeaveTypesCmd.class)
public class EditLeaveTypesCmdIntercept extends AbstractCommandProxy<Map<String,Object>> {

    BaseBean basebean = new BaseBean();

    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> command) {
        EditLeaveTypesCmd cmd = (EditLeaveTypesCmd) command;
        Map<String, Object> params = cmd.getParams();
        Map<String, Object> result = nextExecute(cmd);
        RecordSet rsQ = new RecordSet();
        RecordSet rsU = new RecordSet();
        User user = cmd.getUser();
        String foreignLeave = Util.null2String(params.get("foreignLeave"));
        basebean.writeLog("foreignLeave:"+foreignLeave);
        String ruleId = Util.null2String(params.get("typeId"));
        basebean.writeLog("ruleId:"+ruleId);
        if ( StringUtils.isNotBlank( foreignLeave)) {
            if ( "1".equals(foreignLeave) ) {//假期类型是外籍类型
                //将该外籍假期类型对应的假期类型记录
                String leaveType = Util.null2String(params.get("leaveType"));
                String moSql = "update kq_LeaveRules set corresLeaveType = ?,isForg = 1 where id = ?";
                rsU.executeUpdate(moSql,leaveType,ruleId);

                String hrmResourceIds= Util.null2String(params.get("hrmResourceId"));
                if ( StringUtils.isNotBlank(hrmResourceIds)) {
                    List < Map < String, Object>> resList = new ArrayList<>();

                    String getResInfos = "select workcode, subcompanyid1, departmentid from hrmresource where id = ?";
                    String[] idsArr = hrmResourceIds.split(",");
                    for (String id : idsArr) {
                        rsQ.executeQuery(getResInfos,id);
                        while (rsQ.next()) {
                            String workcode = Util.null2String(rsQ.getString("workcode"));
                            String departmentid = Util.null2String(rsQ.getString("departmentid"));
                            String subcompany = Util.null2String(rsQ.getString("subcompanyid1"));
                            if ( StringUtils.isNotBlank(workcode) && StringUtils.isNotBlank(departmentid) && StringUtils.isNotBlank(subcompany)) {
                                Map < String, Object> resMap = new HashMap<>();
                                resMap.put("resourceId",id);
                                resMap.put("workcode",workcode);
                                resMap.put("departmentid",departmentid);
                                resMap.put("subcompany",subcompany);
                                resList.add(resMap);
                            }
                        }
                    }
                    basebean.writeLog("resList:"+resList);

                    //获取该假期类型当前已经存在的外籍人员
                    List<String> excList = new ArrayList<>();
                    String acqSql = "select ygxm from uf_jqlxb where jqlx = ? and isdelete = 1";
                    rsQ.executeQuery(acqSql,ruleId);
                    while (rsQ.next()) {
                        String ygxm = Util.null2String(rsQ.getString("ygxm"));
                        if (StringUtils.isNotBlank(ygxm)) {
                            excList.add(ygxm);
                        }
                    }

                    if ( !resList.isEmpty()) {

                        if ( !excList.isEmpty()) {
                            List<String> diffList = new ArrayList<>(excList);
                            diffList.removeAll(Arrays.asList(idsArr));
                            if ( !diffList.isEmpty()) {
                                String removeSql = "update uf_jqlxb set isdelete = 0 where ygxm = ? ";
                                for (String id : diffList) {
                                    rsU.executeUpdate(removeSql,id);
                                }

                            }
                        }

                        //获取假期类型建模表模块id
                        String leaveTypeModeId = Util.null2String(basebean.getPropValue("propExtends", "leaveTypeModeId"));
                        basebean.writeLog("leaveTypeModeId",leaveTypeModeId);
                        String nowDate = DateUtil.format(new Date(),"yyyy-MM-dd");
                        String nowTime = DateUtil.format(new Date(),"HH:mm:ss");

                        String checkResExistSql= "select count(*) as number from uf_jqlxb where jqlx = ? and ygxm = ? and isdelete = 1";
                        for ( Map< String, Object> resInfos: resList) {
                            String resourceId = Util.null2String(resInfos.get("resourceId"));
                            String workcode = Util.null2String(resInfos.get("workcode"));
                            String departmentid = Util.null2String(resInfos.get("departmentid"));
                            String subcompany = Util.null2String(resInfos.get("subcompany"));
                            rsQ.executeQuery(checkResExistSql,ruleId,resourceId);
                            while (rsQ.next()) {
                                Integer number = Util.getIntValue(Util.null2String(rsQ.getString("number")));
                                if (number <= 0) {//不存在就插入
                                    String insertResSql = "insert into uf_jqlxb (ygxm,ygbh,jqlx,ygszfb,ygszbm,isdelete," +
                                            "formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime) " +
                                            "values (?,?,?,?,?,?,?,?,?,?,?)";
                                    rsU.executeUpdate(insertResSql,resourceId,workcode,ruleId,subcompany,departmentid,"1",
                                            leaveTypeModeId,user.getUID(),user.getType(),nowDate,nowTime);
                                }
                            }
                        }
                        //权限重构
                        List<String> idsList = new ArrayList<>();
                        String acqIdSql = "select id from uf_jqlxb where modedatacreatedate = ? and modedatacreatetime = ? ";
                        rsQ.executeQuery(acqIdSql,nowDate,nowTime);
                        while (rsQ.next()) {
                            String id = Util.null2String(rsQ.getString("id"));
                            if (StringUtils.isNotBlank(id)) {
                                idsList.add(id);
                            }
                        }
                        if ( !idsList.isEmpty()) {
                            for (String id:idsList) {
                                ModeRightInfo modeRightInfo = new ModeRightInfo();
                                modeRightInfo.setNewRight(true);
                                modeRightInfo.editModeDataShare(user.getUID(), Util.getIntValue(leaveTypeModeId), Util.getIntValue(id));//新建的时候添加共享
                            }
                        }


                    } else {
                        if ( !excList.isEmpty()) {

                            String removeSql = "update uf_jqlxb set isdelete = 0 where ygxm = ? ";
                            for (String id : excList) {
                                rsU.executeUpdate(removeSql,id);
                            }

                        }
                    }

                }
            } else {
                String moSql = "update kq_LeaveRules set corresLeaveType = ?,isForg = 0 where id = ?";
                rsU.executeUpdate(moSql,null,ruleId);
            }
        }

        return result;
    }

}
